using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace RPGOnline.Remoting.Entities
{
    [DataContract]
    public class ChatGroup
    {
        public ChatGroup(string chatGroupName, Guid id)
        {
            Id = id;
            Name = chatGroupName;
            Users = new Dictionary<Guid, User>();
        }

        [DataMember]
        public Guid Id { get; private set; }
        [DataMember]
        public string Name { get; private set; }
        [DataMember]
        public Dictionary<Guid, User> Users { get; private set; }

        public void Add(User user)
        {
            Users.Add(user.Id, user);
        }

        public void Remove(User user)
        {
            Users.Remove(user.Id);
        }

        public bool Contains(Guid userId)
        {
            return Users.ContainsKey(userId);
        }

        public bool Contains(string userName)
        {
            return FindUserByName(userName) != null;
        }

        public User FindUserByName(string userName)
        {
            return Users.Values.FirstOrDefault(
                u => u.Name.Equals(userName, StringComparison.InvariantCultureIgnoreCase)
            );
        }

        public User FindByCharacterName(string characterName)
        {
            return Users.Values.FirstOrDefault(
                u => u.CharacterName == null ? false : u.CharacterName.Equals(characterName, StringComparison.InvariantCultureIgnoreCase)
            );
        }
    }
}